package com.mall.service.impl;

import com.mall.common.BusinessException;
import com.mall.entity.Category;
import com.mall.mapper.CategoryMapper;
import com.mall.mapper.ProductMapper;
import com.mall.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Category> getCategoryTree() {
        return categoryMapper.selectCategoryTree();
    }

    @Override
    public List<Category> getEnabledCategoryTree() {
        return categoryMapper.selectEnabledCategories();
    }

    @Override
    public Category getCategoryById(Long categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    @Override
    public List<Category> getCategoriesByParentId(Long parentId) {
        return categoryMapper.selectByParentId(parentId);
    }

    @Override
    public List<Category> getCategoriesByLevel(Integer level) {
        return categoryMapper.selectByLevel(level);
    }

    @Override
    @Transactional
    public void createCategory(Category category) {
        // 检查同级分类下是否存在同名分类
        if (categoryMapper.existsByName(category.getName(), category.getParentId())) {
            throw new BusinessException("同级分类下已存在同名分类");
        }
        
        // 设置默认值
        if (category.getStatus() == null) {
            category.setStatus(1); // 默认启用
        }
        if (category.getSort() == null) {
            category.setSort(0); // 默认排序值
        }
        
        categoryMapper.insert(category);
    }

    @Override
    @Transactional
    public void updateCategory(Category category) {
        Category existingCategory = categoryMapper.selectById(category.getId());
        if (existingCategory == null) {
            throw new BusinessException("分类不存在");
        }
        
        // 检查同级分类下是否存在同名分类(排除自身)
        if (!existingCategory.getName().equals(category.getName()) && 
            categoryMapper.existsByName(category.getName(), category.getParentId())) {
            throw new BusinessException("同级分类下已存在同名分类");
        }
        
        categoryMapper.updateById(category);
    }

    @Override
    @Transactional
    public void deleteCategory(Long categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        
        // 检查是否有子分类
        if (categoryMapper.hasChildren(categoryId)) {
            throw new BusinessException("该分类下存在子分类，无法删除");
        }
        
        // 检查分类下是否有商品
        if (productMapper.countByCategoryId(categoryId) > 0) {
            throw new BusinessException("该分类下存在商品，无法删除");
        }
        
        categoryMapper.deleteById(categoryId);
    }

    @Override
    @Transactional
    public void deleteCategories(Long[] categoryIds) {
        if (categoryIds != null && categoryIds.length > 0) {
            List<Long> idList = new ArrayList<>();
            for (Long categoryId : categoryIds) {
                // 检查是否有子分类
                if (categoryMapper.hasChildren(categoryId)) {
                    Category category = categoryMapper.selectById(categoryId);
                    throw new BusinessException("分类【" + category.getName() + "】下存在子分类，无法删除");
                }
                
                // 检查分类下是否有商品
                if (productMapper.countByCategoryId(categoryId) > 0) {
                    Category category = categoryMapper.selectById(categoryId);
                    throw new BusinessException("分类【" + category.getName() + "】下存在商品，无法删除");
                }
                
                idList.add(categoryId);
            }
            categoryMapper.deleteBatchByIds(idList);
        }
    }

    @Override
    @Transactional
    public void updateCategoryStatus(Long categoryId, Integer status) {
        Category category = categoryMapper.selectById(categoryId);
        if (category == null) {
            throw new BusinessException("分类不存在");
        }
        categoryMapper.updateStatus(categoryId, status);
    }

    @Override
    public boolean existsByName(String name, Long parentId) {
        return categoryMapper.existsByName(name, parentId);
    }

    @Override
    public boolean hasChildren(Long categoryId) {
        return categoryMapper.hasChildren(categoryId);
    }

    @Override
    public boolean hasProducts(Long categoryId) {
        return productMapper.countByCategoryId(categoryId) > 0;
    }
}